/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package requestDataPacket;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author THANG
 */
public class requestData extends Thread {
            /*private ServerSocket  e; // dung tcp
            private Socket f; //dung tcp*/
    private DatagramSocket socket;//dung udgp
    private InetAddress address;
    private String request;
    private boolean stillRuning = true;
    private int port;
    
    public requestData() throws UnknownHostException, SocketException
    {
        this.address = InetAddress.getByName("127.0.0.1");
        this.port = 10000;
        this.request = "String"; 
        this.socket = new DatagramSocket();
        
    }
    
    public requestData(String Data, String address, int port ) throws UnknownHostException, SocketException
    {
        this.address = InetAddress.getByName(address);
        this.port = port;
        this.request = Data; 
        this.socket = new DatagramSocket();
        
    }
    
    @Override
    public void run()
    {
        while(stillRuning)
        {
            byte[] buff = new byte[1024];
            buff = this.request.getBytes();
            DatagramPacket p = new DatagramPacket(buff, buff.length, address, port);
            try {
                socket.send(p);
                
                this.stillRuning = false;
            } catch (IOException ex) {
                Logger.getLogger(ListeningThread.ListeningThread.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }


}
